package com.roadrunner;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

	 

	
public class listOrderFragment extends ListFragment implements FragmentCommunicator
{
	public Context context;
	OnHeadlineSelectedListener mCallback;
	private ActivityCommunicator activityCommunicator;
	
	// The container Activity must implement this interface so the frag can deliver messages
    public interface OnHeadlineSelectedListener 
    {
        /** Called by HeadlinesFragment when a list item is selected */
        public void onArticleSelected(int position);
    }
    
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	 }

	 @Override
	 public void onStart() 
	 {
		 super.onStart();

		 // When in two-pane layout, set the listview to highlight the selected list item
		 // (We do this during onStart because at the point the listview is available.)
		 if (getFragmentManager().findFragmentById(R.id.orderDetails) != null) 
		 {
	        	getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	     }
	 }
	 
	 @Override
	 public void onAttach(Activity activity)
	 {
		 super.onAttach(activity);

		 // This makes sure that the container activity has implemented
		 // the callback interface. If not, it throws an exception.
		 try {
			 mCallback = (OnHeadlineSelectedListener) activity;
		 } catch (ClassCastException e) {
			 throw new ClassCastException(activity.toString()
					 + " must implement OnHeadlineSelectedListener");
		 }
		 
		context = getActivity();
		activityCommunicator = (ActivityCommunicator)context;
		((JobsList)context).fragmentCommunicator = this;
	 }

	 @Override
	 public void onListItemClick(ListView l, View v, int position, long id) 
	 {
		 // Notify the parent activity of selected item
		 mCallback.onArticleSelected(position);
	        
		 // Set the item as checked to be highlighted when in two-pane layout
		 getListView().setItemChecked(position, true);
	 }

	@Override
	public void passDataToFragment(String someValue) {
		
		int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
		
		// TODO Auto-generated method stub
		ArrayList<Job> jobs =  JobsList.getJobList();	    
		ArrayList<String> orderIDs = new ArrayList<String>();
		
		for (Job order : jobs)
		{
			String orderName = new String();
			
			if(order.getJobState().compareTo("PICKED_UP") == 0)
				orderName = order.getDestAdress();
			else
				orderName = order.getExpAdress();
			
			orderIDs.add(orderName);
		}
		
		StableArrayAdapter adapter = new StableArrayAdapter(getActivity(), layout, orderIDs, jobs);
		setListAdapter(adapter);
	}
}
